import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/data/BankEntity.dart';
import 'package:mtf/app/data/BannerEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:mtf/app/theme/app_colors.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class withdrawalController extends BaseController {

  var choiceBankId="";

  var preAccount="0.00".obs;

  var bankNameObs="请选择银行卡".obs;
  var mt5BalanceObs="".obs;

  TextEditingController amountController = TextEditingController();

  void reqAllAmount() async{
    var resp=await HttpManager.reqUserInfo();
    if(resp.isSuccessData()){
      mt5BalanceObs.value=resp.data!.mt5Balance ??"0.00";
    }
  }

  void withdrawAll(){
    amountController.text=mt5BalanceObs.value;
  }

  @override
  void onInit() {
    super.onInit();
    reqAllAmount();
    amountController.addListener(() {
      if(amountController.text.isNotEmpty){
        print('输入了：${amountController.text}');
        reqRechargeRule(amountController.text);
      }else{
        preAccount.value="0.00";
      }

    });
  }

  void clickSub() async{
    var amount=amountController.text;
    if(choiceBankId.isEmpty){
      SmartDialog.showToast("请选择银行卡");
    }else if(amount.isEmpty){
      SmartDialog.showToast("请输入提款金额");
    }else{
     var resp= await HttpManager.withdraw(amount, choiceBankId);
     if(resp.isSuccess()){
       Get.toNamed(Routes.SUCCESS, arguments: {
         "title": "提款成功",
         "desc": "等待平台审核，请耐心等候",
         "type": 1,
         "buttonText": "返回资产"
       });
     }
    }

  }

  void bankList() async {
    showLoading();
    var resp = await HttpManager.reqBankList();
    hideLoading();
    if (resp.isSuccessData()&&!resp.data!.list.isEmpty) {
      showBankDialog(resp.data!.list);
    }
  }

  void showBankDialog(List<ListBankElement> list) {
    showModalBottomSheet(
        isScrollControlled: true,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))
        ),
        context: Get.context!!,
        builder: (context) {
          return SingleChildScrollView(
            child: Container(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.all(15),
                    child: Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerRight,
                          child: Icon(
                            Icons.close,
                            color: c_999999,
                          ),
                        ),
                        Center(
                            child: Text(
                              "选择到账银行卡",
                              style: TextStyle(color: c_333333, fontSize: 16),
                            ))
                      ],
                    ),
                  ),
                  ListView.separated(
                    physics: NeverScrollableScrollPhysics(),
                    separatorBuilder: (context, index) {
                      return Divider(height: 1);
                    }
                    ,itemBuilder: (context, index) {
                    if(index==list.length){
                      return ListTile(
                        minLeadingWidth: 0,
                        leading: Icon(Icons.add_circle_outline_sharp,color: c_bbbbbb,),
                        title: Text("使用新卡提现",style: TextStyle(color: c_333333,fontSize: 15),),
                      );
                    }else{
                      var bean=list[index];
                      return InkWell(
                        onTap: (){
                          choiceBankId=bean.id.toString();
                          bankNameObs.value=bean.bankName ??"";
                          Get.back();
                        },
                        child: ListTile(
                          minLeadingWidth: 0,
                          // leading: Icon(Icons.add_circle_outline),
                          title: Text("${bean.bankName}(${bean.bankAccount})",style: TextStyle(color: c_333333,fontSize: 15),),
                        ),
                      );
                    }
                  },itemCount: list.length+1,shrinkWrap: true,)
                ],
              ),
            ),
          );
        });
  }

  void reqRechargeRule(String money) async{
    var resp= await HttpManager.reqMoneyAccount(2, money);
    if(resp.isSuccessData()){
      preAccount.value=resp.data!.amount;
    }
  }



}
